package com.winit.deploy.task.androidTask;

import com.winit.core.task.Task;
import com.winit.deploy.bean.DeployEnum;
import com.winit.deploy.bean.WinitAndroidInfo;
import com.winit.deploy.server.WinitAndroidServer;
import com.winit.deploy.util.DeployUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 安卓工程安装
 * Created by wangjian on 16/10/17.
 */
public class AndroidInstallTask extends Task {

    private static Logger logger = LoggerFactory.getLogger(AndroidInstallTask.class);

    private WinitAndroidServer server;


    public AndroidInstallTask(String taskId, Integer expireTime, WinitAndroidServer server) {
        super(taskId, expireTime);
        this.server = server;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.androidTask.AndroidInstallTask#call start taskId="+this.taskId);
        WinitAndroidInfo info = this.server.getAndroidInfo();
        String projectName = info.getProjectName();
        String editionNum = info.getEditionNum();

        boolean flag = this.server.doInstall("apache");

        if(flag){
            if(this.taskId.startsWith("staging")){
                DeployUtil.updateProjectVersion(projectName,editionNum, DeployEnum.STAGING);
            }
            if(this.taskId.startsWith("online")){
                DeployUtil.updateProjectVersion(projectName,editionNum,DeployEnum.ONLINE);
                DeployUtil.cpOnlineVersionToNas(projectName,editionNum);
            }
        }
        logger.info("com.winit.deploy.task.androidTask.AndroidInstallTask#call end taskId="+this.taskId);
        return flag?TaskStatus.STATUS_SUCCESS:TaskStatus.STATUS_ERROR;
    }
}
